﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using DevExpress.XtraGrid.Columns;
using System.Reflection;
using Ebada.Client;
using DevExpress.XtraGrid.Views.Base;
using Ebada.Client.Platform;
using Ebada.UI.Base;
namespace Ebada.Modules.Vehicle
{

    public partial class UCVehicleDispatch_Left : DevExpress.XtraEditors.XtraUserControl {
        private GridViewOperation<Vehicles> gridViewOperation;

        public event SendDataEventHandler<Vehicles> FocusedRowChanged;
        public event SendDataEventHandler<Vehicles> SelectionChanged;
        public List<string> select_list = new List<string>();
        public bool ISRefresh = false;
        public static bool FirstLoad = false;
        public UCVehicleDispatch_Left() 
        {
            InitializeComponent();
          
            gridViewOperation = new GridViewOperation<Vehicles>(gridControl1, gridView1, barManager1,new frmVehiclesEdit());
            gridViewOperation.CreatingObjectEvent +=gridViewOperation_CreatingObjectEvent;
            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridViewOperation_FocusedRowChanged);
            initColumns();
        }

       void gridViewOperation_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) 
        {
            if (FocusedRowChanged != null)
                FocusedRowChanged(gridView1, gridView1.GetFocusedRow() as Vehicles);
        }
       
        /// <summary>
        /// 设置隐藏列
        /// </summary>
        /// 
       public  void initColumns() 
        {
            for (int i = 0; i < gridView1.Columns.Count; i++)
            {
                gridView1.Columns[i].Visible = false;
            }
            int m = 0;
            gridView1.Columns["VehicleState"].Visible = true;
            gridView1.Columns["VehicleState"].VisibleIndex = m++;
            gridView1.Columns["VehicleCode"].Visible = true;
            gridView1.Columns["VehicleCode"].VisibleIndex = m++;
            gridView1.Columns["VehicleNumber"].Visible = true;
            gridView1.Columns["VehicleNumber"].VisibleIndex = m++;
            gridView1.Columns["EmployeeName"].Visible = true;
            gridView1.Columns["EmployeeName"].VisibleIndex = m++;
            gridView1.Columns["VehicleType"].Visible = true;
            gridView1.Columns["VehicleType"].VisibleIndex = m++;
         
          
        }
      
    
        /// <summary>
        /// 封装了数据操作的对象
        /// </summary>
        [Browsable(false)]
       public GridViewOperation<Vehicles> GridViewOperation
       {
            get { return gridViewOperation; }
            set { gridViewOperation = value; }
        }
        /// <summary>
        /// 新建对象设置Key值
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_CreatingObjectEvent(Vehicles newobj)
        {
            //newobj.VehicleID = Guid.NewGuid().ToString();
            //newobj.VehicleState = "待命";
           
        }
        
        public void InitData()
        {
            string Employee_id = MainHelper.UserBaseX.Empolyee_ID;
            string Org_ID = MainHelper.UserBaseX.Org_ID;

            string sql = "where OrgCode=(select OrgCode from mOrg where Org_ID='" + Org_ID + "')  and (VehicleState = '待命' or  VehicleState = '出车') order by VehicleCode";

            if (MainHelper.UserBaseX.Empolyee_Name!="")
            {
                gridViewOperation.RefreshData(sql);
            }
            else
            {
                gridViewOperation.RefreshData(" where  VehicleState = '待命' or  VehicleState = '出车' order by VehicleCode");

            }
        }
       
        private FormBase showControl(UserControl uc)
        {
            FormBase dlg = new FormBase();
           
            dlg.Controls.Add(uc);
            uc.Dock = DockStyle.Fill;
            dlg.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            dlg.Show();
            return dlg;
        }
        //调度
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.RowCount==0)
	        {
                 return;
	        }
            Vehicles tempvhi=(Vehicles)gridView1.GetFocusedRow();
            //if (tempvhi.VehicleState=="出车")
            //{
            //    MessageBox.Show("请选择状态为待命的车辆进行调度！");
            //    return;
            //}
            frmVehicleDispatchEdit frm = new frmVehicleDispatchEdit();
            VehicleDispatch temp = new VehicleDispatch();
            string tempstr = DateTime.Now.ToShortDateString() + "_" + tempvhi.VehicleNumber + "_";
            if (MainHelper.PlatformSqlMap.GetRowCount<VehicleDispatch>("where 2>1")<10)
            {
               tempstr+="0"+ MainHelper.PlatformSqlMap.GetRowCount<VehicleDispatch>("where 2>1");
            }
            else
	        {
               tempstr+= MainHelper.PlatformSqlMap.GetRowCount<VehicleDispatch>("where 2>1");
	        }
            temp.VehicleDispatchID = tempstr;
            temp.VehicleID = tempvhi.VehicleID;
            temp.VehicleNumber = tempvhi.VehicleNumber;
            temp.VehicleType = tempvhi.VehicleType;
            mOrg temport = MainHelper.PlatformSqlMap.GetOneByKey<mOrg>(MainHelper.UserBaseX.Org_ID);
            if (temport!=null)
            {
                temp.UsingOrgCode = temport.OrgCode;
                temp.UsingOrgName = temport.cnName;
            }
            temp.UsingDeptName = tempvhi.UsingOrgName;
            temp.EmployeeNo = tempvhi.EmployeeNo;
            temp.EmployeeName = tempvhi.EmployeeName;
            Empolyees tempemp=MainHelper.PlatformSqlMap.GetOneByKey<Empolyees>(temp.EmployeeNo);
            if (tempemp!=null)
            {
                temp.PhoneNum = tempemp.Tel;
            }
           
           
            string sql = "where VehicleID='" + temp.VehicleID +"' and EndKilometer=0";
            //查出该车的所有派车计划（已回车的除外）
           IList < VehicleDispatch > oldvdlist = MainHelper.PlatformSqlMap.GetListByWhere<VehicleDispatch>(sql);
           //记录派车计划中预计回车时间最大值  作为当前派车计划的预计出车时间（无历史则以当前时间为准）
            DateTime oldtime=DateTime.Now;
           for (int i = 0; i < oldvdlist.Count; i++)
           {
               if (oldtime.Date.CompareTo(oldvdlist[i].ExpectReturnTime.Date)<0)
               {
                   oldtime = oldvdlist[i].ExpectReturnTime;
               }
           }
           temp.ExpectDispatchingTime = oldtime;
           temp.ExpectReturnTime = oldtime.AddDays(1);
            temp.UsingDays = 1;
            temp.Reason = "拉货";
            //如果有车主司机则记录
            VehicleDispatchEmp tempvde = new VehicleDispatchEmp();
            //tempvde.VehicleDispatchEmpID = Guid.NewGuid().ToString();
            if (((Vehicles)gridView1.GetFocusedRow()).EmployeeNo!="")
            {
               
                tempvde.VehicleDispatchID = temp.VehicleDispatchID;
                tempvde.EmployeeNo = ((Vehicles)gridView1.GetFocusedRow()).EmployeeNo;
                tempvde.EmployeeName = ((Vehicles)gridView1.GetFocusedRow()).EmployeeName;
                MainHelper.PlatformSqlMap.Create<VehicleDispatchEmp>(tempvde);
                temp.EmployeeNum = 1;
            }
            
            frm.RowData = temp;
            if ( frm.ShowDialog()==DialogResult.OK)
            {
                MainHelper.PlatformSqlMap.Create<VehicleDispatch>((VehicleDispatch)frm.RowData);
                //Vehicles tempvehic = MainHelper.PlatformSqlMap.GetOneByKey<Vehicles>(temp.VehicleID);
                //tempvehic.VehicleState = "出车";
                //if (tempvehic.VehicleImage==null)
                //{
                //    tempvehic.VehicleImage = new byte[0];
                //}
                //MainHelper.PlatformSqlMap.Update<Vehicles>(tempvehic);
                //InitData();
                UCVehicleDispatch.ParentForm.ucVehicleDispatch_Right1.Refresh(select_list);
            }
            else
            {
                MainHelper.PlatformSqlMap.Delete<VehicleDispatchEmp>(tempvde);
            }
           

        }

        private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            if (ISRefresh)
            {
                ISRefresh = false;
                return;
            }
            select_list.Clear();
            for (int i = 0; i < gridView1.GetSelectedRows().Length; i++)
            {
               string tempstr= ((Vehicles)gridView1.GetRow(gridView1.GetSelectedRows()[i])).VehicleID;
               select_list.Add(tempstr);
            }
            UCVehicleDispatch.ParentForm.ucVehicleDispatch_Right1.Refresh(select_list);
        }

      

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            InitData();
        }
       

       

     

    }
}
